package com.lee.dhsg.dao;


import com.lee.dhsg.dao.provider.CourseSqlProvider;
import com.lee.dhsg.domain.Category;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author Jun
 */
@Component
@Mapper
public interface CategoryDao {

    /**
     * 通过父级类别id 查询子级类别id
     *
     * @author Lee
     */
    @Select("select categoryid from category WHERE fcategoryid=#{fcategoryid}")
    List<Integer> selectByFcategoryid(Integer fcategoryid);

    /**
     * 通过id查询课题
     * @param id
     * @return Category
     */
    @Select("select * from category where categoryid=#{id}")
    Category selectById(Integer id);

    /**
     * 查询所有类型
     * @return List<Category>
     */
    @Select("select * from category")
    List<Category> findAll();

    /**
     * 查询所有一级分类
     * @return
     */
    @Select("select * from category where fcategoryid is null")
    List<Category> findMain();

    /**
     * 查询所有一级分类或者某课程分类
     *@param params
     * @return List<Category>
     */
    @Select("select * from category where fcategoryid is null and ")
    @Results({
            @Result(id=true,column ="categoryid",property = "categoryid"),
            @Result(column = "fcategoryid",property = "fcategoryid"),
            @Result(column = "name",property = "name"),
            @Result(column = "description",property = "description"),
            @Result(column = "courseid",property = "course",
                    one=@One(
                            select = "com.dhsg.dao.CourseDao.selectById",
                            fetchType = FetchType.EAGER
                    ))
    })
    List<Category> findByName(Map<String, Object> params);

    /**
     * 查询某父分类下的子分类
     *@param categoryid
     * @return List<Category>
     */
    @Select("select b.* from category as a join category as b on a.categoryid=b.fcategoryid WHERE a.categoryid=#{id}")
    List<Category> findChild(Integer categoryid);

    /**
     * 查询某分类By分类名
     * @param name
     * @return
     */
    @Select("select * from category where name like concat ('%',#{name},'%')")
    @Results({
            @Result(column = "categoryid",property = "categoryid"),
            @Result(column = "fcategoryid",property = "fcategoryid"),
            @Result(column = "name",property = "name"),
            @Result(column = "description",property = "description"),
            @Result(column = "categoryid",property = "courseList",
                    many=@Many(
                            select = "com.dhsg.dao.CourseDao.selectBycategoryid",
                            fetchType = FetchType.LAZY
                    ))
    })
    List<Category> selectOneByName(String name);

}
